//
//  ListCollectionViewController.m
//  XiangCaoJiaYuan
//
//  Created by tongj on 2017/8/24.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "ListCollectionViewController.h"
#import "ListCollectionViewCell.h"
#import "UIButton+LT.h"
#import "ListprizeModel.h"
@interface ListCollectionViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource,MJRefreshBaseViewDelegate>
{
    NSMutableArray * datas;
    UICollectionView * _collectionView;
    UILabel *count1;
    UILabel *count2;
    int a;
    MJRefreshHeaderView *_headerView;
    MJRefreshFooterView *_footerView;
    BOOL _isLoad;//是否处于网络请求状态
    NSInteger _pageIndex;
}
@property (nonatomic,strong)NSMutableArray *dataArray1;

@end

@implementation ListCollectionViewController

//static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray1  = [[NSMutableArray alloc]init];
    _pageIndex = 0;
    _isLoad =NO;
   
    [self questData];

   self.title = @"领奖记录";
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 15, 25)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"香草园地-发布信息_03@2x"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(goBacktoUp) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backItem;
    [self createCollectionView];
}
-(void)goBacktoUp{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)questData{

    NSString *url = [NSString stringWithFormat:@"%@prize/winners?prize_id=%@&times=%ld",ServiceURL2,_paizeid,_pageIndex];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [NSMutableArray array];
        NSDictionary *pri = dict[@"data"];
        NSArray *str11 = pri[@"prizeWinnerInfoList"];

        NSString *aaa = dict[@"receiveCount"];
        a = [aaa intValue];
        for (NSDictionary *dict1 in str11)
        {
            [array addObject:dict1];
        }
        if (_pageIndex==0) {
            [_dataArray1 removeAllObjects];
        }
        NSArray *array2 = [ListprizeModel arrayOfModelsFromDictionaries:array error:nil];
        [_dataArray1 addObjectsFromArray:array2];
        [_collectionView reloadData];
        _isLoad = NO;
        [_headerView endRefreshing];
        [_footerView endRefreshing];
        
    } failed:^(NSString *error) {
        
    }];
    
}
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (_isLoad) {
        return;
    }
    _isLoad = YES;
    //如果是下拉刷新
    
    
    if (refreshView==_headerView) {
        _pageIndex=0;
    }else if (refreshView==_footerView){
        _pageIndex++;
    }
    [self questData];
    
}
- (void)dealloc {
    [_headerView free];
    [_footerView free];
}
- (void)createCollectionView{
    
    
    UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, 50*HEIGHT)];
    view.backgroundColor = kRGBColor(240,240,240);
    [self.view addSubview:view];
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
    count1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, UIScreen_WIDTH/2, 20)];
    count1.textAlignment = NSTextAlignmentCenter;
    count1.textColor = coloreg2;
    count1.text = [NSString stringWithFormat:@"投放个数：%d个",_prize_num];
    [view addSubview:count1];
    count2 = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2, 15, UIScreen_WIDTH/2, 20)];
    count2.textAlignment = NSTextAlignmentCenter;
    count2.textColor = coloreg2;
    count2.text = [NSString stringWithFormat:@"已领取个数：%d个",a];
    [view addSubview:count2];
//    flowLayout.minimumLineSpacing = 10;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, view.frame.size.height, UIScreen_WIDTH, UIScreen_HEIGHT-view.frame.size.height-50) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.alwaysBounceVertical = YES;
    _collectionView.backgroundColor = colordfdfdf;
    //注册cell
    UINib * nib = [UINib nibWithNibName:@"ListCollectionViewCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"ListCollectionViewCell"];
    
    [self.view addSubview:_collectionView];
    _headerView=[MJRefreshHeaderView header];
    _headerView.scrollView=_collectionView;
    _headerView.delegate=self;
    //上拉加载更多的视图
    _footerView=[MJRefreshFooterView footer];
    _footerView.scrollView=_collectionView;
    _footerView.delegate=self;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    NSLog(@"%ld",_dataArray1.count);
//
//    return _dataArray1.count/3;
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _dataArray1.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListCollectionViewCell" forIndexPath:indexPath];
    ListprizeModel *model = _dataArray1[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
       return CGSizeMake(UIScreen_WIDTH/4.0,125);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(self.view.bounds.size.width,0);
    }
    return CGSizeMake(self.view.bounds.size.width,10);
    
}

#pragma mark <UICollectionViewDelegate>
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0,1,0,0);//分别为上、左、下、右
//  
//}
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
